﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.DAL;
using WXYW.BLL;

namespace WXYW
{
    public partial class PublishPost : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PostService service = new PostService();
                PostTypeDDL.DataSource = service.GetAllPostTypes();
                PostTypeDDL.DataTextField = "Name";
                PostTypeDDL.DataValueField = "ID";
                PostTypeDDL.DataBind();
            }
        }

        protected void PublisBtn_Click(object sender, EventArgs e)
        {
            try
            {
                PostService service = new PostService();
                Post newPost = new Post();
                newPost.Title = TitleTxtBox.Text;
                newPost.Content = ContentTxtBox.Text;
                newPost.Keyword = KeywordsTxtBox.Text;
                newPost.Source = SourceTxtBox.Text;
                newPost.Status = "有效";
                newPost.TargetType = TargetDDL.SelectedValue;
                newPost.PublishDate = DateTime.Now;
                newPost.PostType = service.GetPostType(PostTypeDDL.SelectedValue);
                newPost.AuthorID = ContextService.CurrentAccount.UserID;
                newPost.LastUpdate = DateTime.Now;
                service.AddPost(newPost);

                ClientScript.RegisterStartupScript(this.GetType(), "TipMessage", "alert('公告已成功发布！');", true);
            }
            catch (Exception ex)
            {
 
            }
        }
    }
}
